// 防抖
export function debounce(func, wait, immediate) {
  var timeout, result;
  function debounced () {
    var context = this, args = arguments;
    if (timeout)  clearTimeout(timeout);
    if (immediate) {
      var callNow = !timeout;
      timeout = setTimeout(function() {
        result = func.apply(context, args);
      }, wait);
      if (callNow) result = func.apply(context, args);
    } else {
        timeout = setTimeout(function() {
          result = func.apply(context, args);
        }, wait);
    }
    return result;
  }
  debounced.cancel = function(){
    cleatTimeout(timeout);
    timeout = null;
  }
  return debounced;
}
